﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using FTchina;
using FTchina.DAL;
namespace FTchina.Logic
{
    /// <summary>
    ///UserListLogic 的摘要说明
    /// </summary>
    public class UserListLogic
    {
        public UserListLogic()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


         
        /// <summary>
        /// 删除菜单项（包括子菜单）
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public static Boolean DeleteMenuItem(string MenuID)
        {
            string ids = MenuID + ",";
            getReferMenuIds(MenuID, ref ids);
            ids = ids.TrimEnd(',');

            string sql = "delete from UserList where id in (" + ids + ")";
            return DBHelper.ExecuteSql(sql);
        }

        /// <summary>
        /// 检查是否根菜单
        /// </summary>
        /// <param name="MenuID"></param>
        /// <returns></returns>
        public static Boolean IsRootItem(string MenuID)
        {
            string sql = "SELECT  * FROM  UserList WHERE (ParentID IS NULL) AND (id = @ID) ";
            SqlParameter[] para = new SqlParameter[]
				{					
					new SqlParameter("ID",MenuID)					
				};
            return DBHelper.Testread(sql, para);
        }

        /// <summary>
        /// 修改菜单后更新菜单项的TreeLevel
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="MoveDirection">移动方向</param>
        /// <param name="step">移动步长</param>
        public static void setMenuTreeLevelById(string ID, int TreeLevel)
        {
            string sql = "update UserList set TreeLevel = @TreeLevel where ID =@ID";
            SqlParameter[] para = new SqlParameter[]
				{					
					new SqlParameter("ID",ID)	,
				    new SqlParameter("TreeLevel",TreeLevel)
				};
            DBHelper.ExecuteCommand(sql, para);
        }

        #region Grid内显示树形列表
        /// <summary>
        /// 获取ExtGrid显示树形的DataTable
        /// </summary>
        /// <returns></returns>
        public static DataTable getTreeDataTable()
        {
            DataTable table = new DataTable();
            DataColumn column1 = new DataColumn("Id", typeof(int));
            DataColumn column2 = new DataColumn("Context", typeof(String));
            DataColumn column3 = new DataColumn("TreeLevel", typeof(int));
            DataColumn column5 = new DataColumn("ParentID", typeof(String));
            DataColumn column4 = new DataColumn("paixu", typeof(String));
            table.Columns.Add(column1);
            table.Columns.Add(column2);
            table.Columns.Add(column3);
            table.Columns.Add(column4);
            table.Columns.Add(column5);
            using (DataTable Dt = DBHelper.GetDataTable("select * from UserList   WHERE     (ParentID IS NULL) order by paixu desc "))
            {
                foreach (DataRow dr in Dt.Rows)
                {
                    DataRow row = table.NewRow();
                    row[0] = dr["id"].ToString();
                    row[1] = dr["Context"].ToString();
                    row[2] = dr["TreeLevel"].ToString();
                    row[4] = dr["ParentID"].ToString();
                    row[3] = dr["paixu"].ToString();
                    table.Rows.Add(row);
                    MakeList(table, dr["id"].ToString());
                }
            }
            return table;
        }

        private static void MakeList(DataTable table, string parentid)
        {
            DataTable dts = DBHelper.GetDataTable("select * from UserList   WHERE     (ParentID ='" + parentid + "') order by paixu desc ");
            if (dts.Rows.Count > 0)
            {//如果有子项，那就填进来
                foreach (DataRow dr in dts.Rows)
                {
                    DataRow row = table.NewRow();
                    row[0] = dr["id"].ToString();
                    row[1] = dr["Context"].ToString();
                    row[2] = dr["TreeLevel"].ToString();
                    row[4] = dr["ParentID"].ToString();
                    row[3] = dr["paixu"].ToString();
                    table.Rows.Add(row);

                    DataTable dtxs = DBHelper.GetDataTable("select * from UserList   WHERE     (ParentID ='" + dr["id"].ToString() + "') order by paixu desc");
                    if (dtxs.Rows.Count > 0)
                    {//如果有子项，那就迭代
                        MakeList(table, dr["id"].ToString());
                    }
                }
            }
        }
        #endregion




        #region 下拉列表内显示树形
        /// <summary>
        /// 下拉列表内显示树形
        /// </summary>
        /// <returns></returns>
        public static List<JQueryFeature> getDropDownListTree()
        {
            List<JQueryFeature> myList = new List<JQueryFeature>();
            using (DataTable Dt = DBHelper.GetDataTable("select * from UserList   WHERE     (ParentID IS NULL) order by paixu desc "))
            {
                foreach (DataRow dr in Dt.Rows)
                {
                    myList.Add(new JQueryFeature(dr["id"].ToString(), dr["Context"].ToString(), Convert.ToInt32(dr["TreeLevel"].ToString()), true));

                    MakeList(myList, dr["id"].ToString());
                }
            }

            return myList;
        }

        /// <summary>
        /// 下拉列表内显示树形
        /// </summary>
        /// <param name="myList"></param>
        /// <param name="parentid"></param>
        private static void MakeList(List<JQueryFeature> myList, string parentid)
        {
            DataTable dts = DBHelper.GetDataTable("select * from UserList   WHERE     (ParentID ='" + parentid + "') order by paixu desc ");
            if (dts.Rows.Count > 0)
            {//如果有子项，那就填进来
                foreach (DataRow dr in dts.Rows)
                {
                    myList.Add(new JQueryFeature(dr["id"].ToString(), dr["Context"].ToString(), Convert.ToInt32(dr["TreeLevel"].ToString()), true));


                    DataTable dtxs = DBHelper.GetDataTable("select * from UserList   WHERE     (ParentID ='" + dr["id"].ToString() + "') order by paixu desc");
                    if (dtxs.Rows.Count > 0)
                    {//如果有子项，那就迭代
                        MakeList(myList, dr["id"].ToString());
                    }
                }
            }
        }
        #endregion

        #region 操作菜单列表的树形相关方法
        /// <summary>
        ///int x = 1;
        ///UserListLogic.getTreeLevel("174", ref x);
        ///Alert.ShowInParent(x.ToString());
        /// </summary>
        /// <param name="parentid"></param>
        /// <param name="TreeLevel"></param>
        /// int x=1;
        ///getTreeLevel("174", ref x);
        ///Alert.ShowInParent(x.ToString());
        public static void getTreeLevel(string parentid, ref int TreeLevel)
        {
            if (parentid.Trim() == "0") TreeLevel = 1;
            if (parentid.Trim() != "0")
            {
                string pparent = DBHelper.GetDataTable("select parentid from UserList where id='" + parentid + "'").Rows[0][0].ToString();
                if (pparent.Length > 0)
                {
                    TreeLevel += 1;
                    getTreeLevel(pparent, ref TreeLevel);
                }
            }
        }
        #endregion



        /// <summary>
        /// 获取所有子菜单
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public static void setReferMenuIdsTreeLevel(string MenuID)
        {
            int x = 1;
            getTreeLevel(MenuID, ref x);
            setMenuTreeLevelById(MenuID, x);
            DataTable dt = DBHelper.GetDataTable("select * from UserList where parentid='" + MenuID + "' ");
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    setReferMenuIdsTreeLevel(dr["id"].ToString());
                }
            }

        }


        /// <summary>
        /// 获取所有子菜单ID
        /// </summary>
        /// <param name="MenuID"></param>
        /// <param name="ids"></param>
        public static void getReferMenuIds(string MenuID, ref string ids)
        {
            DataTable dt = DBHelper.GetDataTable("select * from UserList where parentid='" + MenuID + "' ");
            if (dt.Rows.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (DataRow dr in dt.Rows)
                {
                    sb.Append(dr["id"].ToString());
                    sb.Append(",");
                    ids += sb.ToString();
                    getReferMenuIds(dr["id"].ToString(), ref ids);
                }
            }
        }

    }






}